package ar.edu.utn.frsf.tpae.a2013.g03.presentacion.controller.login;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import ar.edu.utn.frsf.tpae.a2013.g03.persistencia.model.Empleado;
import ar.edu.utn.frsf.tpae.a2013.g03.persistencia.model.Supervisor;
import ar.edu.utn.frsf.tpae.a2013.g03.persistencia.model.Usuario;
import ar.edu.utn.frsf.tpae.a2013.g03.service.EmpleadoService;
import ar.edu.utn.frsf.tpae.a2013.g03.service.SupervisorService;

@ManagedBean(name="loginBean")
@SessionScoped
public class LoginBean {
	
		@ManagedProperty(value="#{supervisorService}")
        SupervisorService supervisorService; 
		
		@ManagedProperty(value="#{empleadoService}")
        EmpleadoService empleadoService;  
                        
        private Usuario usuario;

        private String user;
        private String pass;
       
        @PostConstruct
        public void init() {            
    	
        }
        
		public String iniciarSesion() {
			try{
				usuario = supervisorService.iniciarSesion(user, pass);
				if (usuario != null) {
					FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("supervisor", (Supervisor) usuario);
					return "aMenuSupervisor";
				} else {
					usuario = empleadoService.iniciarSesion(user, pass);
					if (usuario != null) {
						FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("empleado", (Empleado) usuario);
						return "aMenuEmpleado";
					}
				}
			} catch (Exception e){
				e.printStackTrace();
				user="";
				pass="";
			}
			return "aLoginFallido";
		}

		public Usuario getUsuario() {
			return usuario;
		}

		public void setUsuario(Usuario usuario) {
			this.usuario = usuario;
		}

		public String getUser() {
			return user;
		}

		public void setUser(String user) {
			this.user = user;
		}

		public String getPass() {
			return pass;
		}

		public void setPass(String pass) {
			this.pass = pass;
		}

		public SupervisorService getSupervisorService() {
			return supervisorService;
		}

		public void setSupervisorService(SupervisorService supervisorService) {
			this.supervisorService = supervisorService;
		}

		public EmpleadoService getEmpleadoService() {
			return empleadoService;
		}

		public void setEmpleadoService(EmpleadoService empleadoService) {
			this.empleadoService = empleadoService;
		}
		       
}

